package com.itheima.service.system.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.UserDao;
import com.itheima.domain.system.User;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.UUIDUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> queryPage(Integer pageNum, Integer pageSize) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = mapper.findAll();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        sqlSession.close();
        return pageInfo;
    }

    @Override
    public List<User> findAll() throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> list = mapper.findAll();
        sqlSession.close();
        return list;
    }

    @Override
    public User findById(String id) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User user = mapper.findById(id);
        sqlSession.close();
        return user;
    }

    @Override
    public void save(User user) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        user.setId(UUIDUtil.getSimpleUUID());
        String password = MD5Util.md5(user.getPassword() + user.getId());
        user.setPassword(password);
        mapper.save(user);
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public void deleteById(String... ids) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        for (String id : ids) {
            mapper.deleteById(id);
        }
        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public void update(User user) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        String password = MD5Util.md5(user.getPassword() + user.getId());
        user.setPassword(password);
        mapper.update(user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public User login(String email, String password) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);


        User user = userDao.findByEmailAndPassword(email, MD5Util.md5(password));

        sqlSession.close();

        return user;
    }

    @Override
    public void updateAuthor(String userId, String... roleIds) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        /*1.删除用户关联的所有的角色*/
        userDao.deleteRolesByUserId(userId);

        /*2.为用户重新绑定角色*/
        if(roleIds!=null && roleIds.length>0){
            for (String roleId : roleIds) {
                userDao.saveRoleAndUser(roleId,userId);
            }
        }

        sqlSession.commit();
        sqlSession.close();
    }
}
